package com.qiall.gateway.service.dubborpc;

import com.qiall.boot.cache.ability.service.CacheService;
import com.qiall.gateway.service.InvokeDubboService;
import org.apache.dubbo.config.ApplicationConfig;
import org.apache.dubbo.config.RegistryConfig;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.stereotype.Component;

@Component
@Configuration
public class RpcInvokerService implements InitializingBean {

    @Value("${dubbo.application.name}")
    private String applicationName;

    @Value("${dubbo.protocol.name}")
    private String protocol;

    @Value("${dubbo.registry.address}")
    private String address;
    @Value("${dubbo.provider.version}")
    private String version;

    @Autowired
    private InvokeDubboService invokeDubboService;


    private DubboRpc rpc;

    @Override
    public void afterPropertiesSet() {
        ApplicationConfig application = new ApplicationConfig(applicationName);
        RegistryConfig registry = new RegistryConfig();
        registry.setProtocol(protocol);
        registry.setAddress(address);
        registry.setTimeout(30000);
        registry.setVersion(version);
        // 初始化类加载器
        rpc = new DubboRpc(application, registry);
        invokeDubboService.initByteClassLoader(rpc);
    }

    @Bean
    public DubboRpc getRpc() {
        return rpc;
    }

}
